package com.changgou.cg.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.cg.service.PageService;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SpuFeign spuFeign;

    //生成静态文件路径
    @Value("${pagePath}")
    private String pagePath;

    /**
     * 构建数据模型
     * @param spuId
     * @return
     */
private Map<String,Object> buildDataModel(Long spuId){
    //构建数据模型
    Map<String,Object> dataMap=new HashMap<>();
//获取spu 和SKU列表
    Result<Spu> result = spuFeign.findById(spuId);
    Spu spuData = result.getData();

    //获取分类信息
    dataMap.put("category1",categoryFeign.findById(spuData.getCategory1Id()).getData());
    dataMap.put("category2",categoryFeign.findById(spuData.getCategory2Id()).getData());
    dataMap.put("category3",categoryFeign.findById(spuData.getCategory3Id()).getData());
    if(spuData.getImages()!=null) {
        dataMap.put("imageList", spuData.getImages().split(","));
    }
    dataMap.put("specificationList", JSON.parseObject(spuData.getSpecItems(),Map.class));
    dataMap.put("spu",spuData);
//根据spuId查询Sku集合
    Sku skuCondition = new Sku();
    skuCondition.setSpuId(spuData.getId());
    Result<List<Sku>> resultSku = skuFeign.findList(skuCondition);
    dataMap.put("skuList",resultSku.getData());
    return dataMap;
}



    /**
     * 根据商品的ID 生成静态页
     *
     * @param spuId
     */
    @Override
    public void createPageHtml(Long spuId) {
// 1.上下文;thymeleaf
        Context context = new Context();
        Map<String, Object> dataModel = this.buildDataModel(spuId);
        context.setVariables(dataModel);
        // 2.准备文件
        File dir = new File(pagePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File dest = new File(dir, spuId + ".html");
        // 3.生成页面
        try (PrintWriter writer = new PrintWriter(dest, "UTF-8")) {
            //执行模板引擎需要传入 模板名、上下文对象以及响应流
            templateEngine.process("cgItem", context, writer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deletePageHtml(Long id) {
        File htmlFile = new File(pagePath+"\\"+id+".html");
        if (htmlFile.exists()){
            htmlFile.delete();
        } else {
            throw new RuntimeException("文件不存在");
        }
    }
}
